import java.util.Scanner


/*
* 索引法，如果需要的数字不存在，就是false
* */
class SolutionThree {
    fun isNStraightHand(hand: IntArray, W: Int): Boolean {
        val size = hand.size
        if (size % W != 0 || size == 0) {
            return false
        }
        val counter = HashMap<Int, Int>()
        hand.forEach {
            // counter.put(it, ( counter[it]?: 0 ) + 1)
            counter[it] = ( counter[it]?: 0 ) + 1
        }
        while (counter.size > 0) {
            val start = counter.keys.minOrNull()!!
            for (i in 0 until W) {
                val key = start + i
                // val value = (counter[start + i]?:0) - 1
                // if (value == 0) {
                //     counter.remove(start + i)
                // } else if (value == -1) {
                //     return false
                // } else {
                //     counter.put(key, value)
                // }
                when (val value = (counter[start + i]?:0) - 1) {
                    0 -> {
                        counter.remove(start + i)
                    }
                    -1 -> {
                        return false
                    }
                    else -> {
                        counter[key] = value
                    }
                }
            }
        }
        return true
    }
}


/*
最笨的方案了。
先排序，拿到第一个，然后循环➕1，remove看结果
* */
class SolutionTwo {
    fun isNStraightHand(hand: IntArray, groupSize: Int): Boolean {
        if (hand.size % groupSize != 0) return false
        hand.sort()
        val handList = hand.toMutableList()
        while (handList.size > 0) {
            var targetStraightCount = 0
            var target = handList[0]
            findTarget@ for (i in handList.indices) {
                if (handList.remove(target)) {
                    targetStraightCount++
                    if (targetStraightCount == groupSize) break@findTarget
                    target++
                } else {
                    return false
                }
            }
        }
        return true
    }
}


/*
排序+哈希计数:
1.代码从小到大排序
2.HashMap计数，value记录每个数字出现的数量
3.循环判断是否能够组成顺子
* */
class Solution {
    fun isNStraightHand(hand: IntArray, groupSize: Int): Boolean {

        if(hand.size % groupSize!=0)
            return false
        val maps = LinkedHashMap<Int,Int>()
        hand.sort()//数组排序
        for(i in hand)//Map计数
            // maps.put(i,(maps.get(i)?:0)+1)
            maps[i] = maps[i]?:0 + 1
        var count = maps.entries.iterator().next().key //从第一位开始
        var les = groupSize
        // while(!maps.isEmpty())
        while(maps.isNotEmpty()){
            if(les == 0){  //完成一组顺子
                les = groupSize
                count = maps.entries.iterator().next().key
            }
            if(maps[count]?:-1 > 0){ //返回-1则代表没有元素来组成顺子
                if(maps[count] ==1)
                    maps.remove(count)
                else
                    maps[count] = (maps[count]?:0)-1
                les--
                count++
            }else
                return false
        }

        return true
    }
}


fun main() {
    val scanner = Scanner(System.`in`)
    val handInput = scanner.nextLine()
    val w = scanner.nextLine().toInt()
    val handMap = handInput?.removeSurrounding(
        "[",
        "]"
    )?.split(",")?.map { it.toInt() }
    if (handMap != null) {
        val hand = handMap.toIntArray()
        println(Solution().isNStraightHand(hand, w))
    }
}
